package logic;

import gui.Screen;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import data.Dao;
import data.User;

public class Bcbs {

	private Screen screen;
	private Dao dao;
	private User user;
	
	public static User currentUser;


	public Bcbs() {
		screen = new Screen();
		screen.setVisible(true);
		dao = new Dao();

	}

	public void run() {
		screen.getLogin().addActionListener(new LoginActionListener());
		screen.getUsermenu().addActionListener(new UsermenuActionListener());
		screen.getShowbalance().addActionListener(new ShowbalanceActionListener());
		screen.getDeposit().addActionListener(new DepositActionListener());
		screen.getWithdraw().addActionListener(new WithdrawActionListener());
		screen.getTransfer().addActionListener(new TransferActionListener());
		screen.getAdminMenu().addActionListener(new AdminMenuActionListener());
		screen.getAddUser().addActionListener(new AddUserActionListener());
		screen.getShowAll().addActionListener(new ShowAllActionListener());



		screen.show(Screen.LOGIN);

	}

	private class LoginActionListener implements ActionListener {

		private int ERROR_MESSAGE;

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getLogin().getBtnContinue())
				if(authenticated() && isAdmin())
					screen.show(Screen.ADMINMENU);
				else if(authenticated())
					screen.show(Screen.USERMENU);
				else 
					JOptionPane.showMessageDialog(screen,"Invalid Username or Password, please try again","Invalid input", ERROR_MESSAGE);
			else if (e.getSource() == screen.getLogin().getMntmAdminMenu())
				screen.show(Screen.ADMINMENU);
			else if (e.getSource() == screen.getLogin().getMntmUserMenu())
				screen.show(Screen.USERMENU);
			else if (e.getSource() == screen.getLogin().getBtnShutDown())
				System.exit(0);
		}
	}

	private class UsermenuActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getUsermenu().getBtnLogOut())
				screen.show(Screen.LOGIN);
			else if (e.getSource() == screen.getUsermenu().getBtnDeposit())
				screen.show(Screen.DEPOSIT);
			else if (e.getSource() == screen.getUsermenu().getBtnWithdraw())
				screen.show(Screen.WITHDRAW);
			else if (e.getSource() == screen.getUsermenu().getBtnTransfer())
				screen.show(Screen.TRANSFER);
			else if (e.getSource() == screen.getUsermenu().getBtnLogOut())
				screen.show(Screen.LOGIN);
			if (e.getSource() == screen.getUsermenu().getBtnShowBalance())
				screen.show(Screen.SHOWBALANCE);

		}
	}

	private class ShowbalanceActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getShowbalance().getBtnMenu())
				if(isAdmin())
				screen.show(Screen.ADMINMENU);
				else if(authenticated())
				screen.show(Screen.USERMENU);
		}

	}

	private class DepositActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getDeposit().getBtnContinue()){
				if(isAdmin())
					screen.show(Screen.ADMINMENU);
				else if(authenticated())
					screen.show(Screen.USERMENU);
				}
			if (e.getSource() == screen.getDeposit().getBtnBack())
				if(isAdmin())
					screen.show(Screen.ADMINMENU);
				else if(authenticated())
					screen.show(Screen.USERMENU);
		}

	}

	private class WithdrawActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getWithdraw().getBtnContinue()){
				if(isAdmin())
					screen.show(Screen.ADMINMENU);
				else if(authenticated())
					screen.show(Screen.USERMENU);}
			if (e.getSource() == screen.getWithdraw().getBtnBack()){
				if(isAdmin())
					screen.show(Screen.ADMINMENU);
				else if(authenticated())
					screen.show(Screen.USERMENU);}
		}

	}

	private class TransferActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getTransfer().getBtnContinue()){
				if(isAdmin())
					screen.show(Screen.ADMINMENU);
				else if(authenticated())
					screen.show(Screen.USERMENU);}
			if (e.getSource() == screen.getTransfer().getBtnBack()){
				if(isAdmin())
					screen.show(Screen.ADMINMENU);
				else if(authenticated())
					screen.show(Screen.USERMENU);}
		}

	}

	private class AdminMenuActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getAdminMenu().getBtnLogOut())
				screen.show(Screen.LOGIN);
			else if (e.getSource() == screen.getAdminMenu().getBtnDeposit())
				screen.show(Screen.DEPOSIT);
			else if (e.getSource() == screen.getAdminMenu().getBtnWithdraw())
				screen.show(Screen.WITHDRAW);
			else if (e.getSource() == screen.getAdminMenu().getBtnTransfer())
				screen.show(Screen.TRANSFER);
			else if (e.getSource() == screen.getAdminMenu().getBtnShowBalance())
				screen.show(Screen.SHOWBALANCE);
			else if (e.getSource() == screen.getAdminMenu().getBtnRichAndPoor())
				screen.show(Screen.DEPOSIT);
			else if (e.getSource() == screen.getAdminMenu().getBtnExchangerate())
				screen.show(Screen.EXCHANGERATE);
			else if (e.getSource() == screen.getAdminMenu().getBtnAddUser())
				screen.show(Screen.ADDUSER);
			else if (e.getSource() == screen.getAdminMenu().getBtnRemoveUser())
				screen.show(Screen.LOGIN);
			else if (e.getSource() == screen.getAdminMenu().getBtnShowAll())
				screen.show(Screen.SHOWALL);
		}
	}

	private class AddUserActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getAddUser().getBtnContinue())
				screen.show(Screen.ADMINMENU);
			else if (e.getSource() == screen.getAddUser().getBtnCancel())
				screen.show(Screen.ADMINMENU);
		}

	}

	private class ShowAllActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getShowAll().getBtnMenu())
				screen.show(Screen.ADMINMENU);
		}

	}

	private class ExchangeRateActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screen.getExchangeRate().getBtnBack())
				screen.show(Screen.ADMINMENU);
			else if (e.getSource() == screen.getExchangeRate().getBtnContinue())
				screen.show(Screen.ADMINMENU);
		}
	}

	public boolean authenticated() {
		boolean authenticated = false;

		String username = screen.getLogin().getUserNameInput().getText();
		String password = screen.getLogin().getPasswordInput().getText();

		for (User user : dao.getUsers()) 
		{
			if (user.getEmail().equals(username)
					&& user.getPassword().equals(password)) 
				authenticated = true;
				currentUser = user.getUserWithUsername(username);
		}
		return authenticated;
	}

	public boolean isAdmin() {
		boolean isAdmin = false;

		String username = screen.getLogin().getUserNameInput().getText();
		String password = screen.getLogin().getPasswordInput().getText();
		for (User user : dao.getUsers()) {
			if (user.getEmail().equals(username)
					&& user.getPassword().equals(password) && user.getIsAdmin() == true)
				isAdmin = true;
		}

		return isAdmin;
	}
}
